package brickBreaker;

import globals.FreeVector;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;


public class BrickArray {
	int startX,startY,rangeX,rangeY;
	float successChance;
	ArrayList<Brick> brickList;
	
	public BrickArray(int sX, int sY, int rX, int rY, float sc){
		startX=sX;	startY=sY;
		rangeX=rX;	rangeY=rY;
		successChance=sc;
		brickList=new ArrayList<Brick>();
		this.init();
	}
	
	void init(){
		for(int iy=startY; iy<rangeY-Brick.brickHeight; iy+=Brick.brickHeight+2){
			for(int ix=startX; ix<rangeX-Brick.brickWidth; ix+=Brick.brickWidth+3){
				if(Math.random()<successChance){ // always fail at 0, always succeed at 1
					brickList.add(new Brick(ix,iy,Color.lightGray));
				}
			}
		}
	}
	void empty(){
		while(!brickList.isEmpty()){
			brickList.remove(0);
		}
	}
	public void reset(){
		empty();
		init();
	}
	
	public void draw(Graphics g){
		for(int i=0; i<brickList.size(); i++){
			brickList.get(i).draw(g);
		}
	}
	
	public void update(Ball ball){
		for(int i=0; i<brickList.size(); i++){
			if(brickList.get(i).doesCollide(ball.x, ball.y, ball.r)){
				brickList.remove(i);
				ball.direction.add(new FreeVector(6,(float)(0.5*Math.PI)));
			}
		}
	}
}
